package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.dao.ActivityDao;
import com.wsd.smartcarwasher.domain.Activity;
import com.wsd.smartcarwasher.domain.Card;
import com.wsd.smartcarwasher.domain.Coupon;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ActivityService {

    @Resource
    private ActivityDao activityDao;
    @Autowired(required = false)
    private CardService cardService;
    @Resource
    private CouponService couponService;
    public List<Activity> getNewActivity() {
        List<Activity>  ls =activityDao.getNewActivity();
        ls.forEach(activity -> {
            String ids = activity.getCouponIds();
            if(StringUtils.isNotBlank(ids)){
                String []  couponIds=ids.split(",");
                activity.setCoupons(activityDao.selectedCoupons(couponIds));
            }
        });
        return  ls;
    }

    public int createActivity(Activity activity) {
        return activityDao.insert(activity);
    }

    public int changeStatus(Activity activity) {
        return activityDao.updateByPrimaryKeySelective(activity);
    }

    public int deleteActivity(int id) {

        return activityDao.delete(id);
    }
    public List<Activity> listActivity()
    {
        //先查出type为2或者3的活动
        List<Activity> activityList=this.activityDao.showPcActivity();
        return activityList;
    }
    public Map<String, Object> showActivity() {

        //先查出type为2或者3的活动
        List<Activity> activityList = this.activityDao.showActivity();
        if (activityList.isEmpty()) {
            return null;
        }

        Map<String, Map<String,Object>> map = new HashMap<>();
        Map<String, Object> activityMap = new HashMap<>();

//        //判断该广告为什么类型，会员卡或者优惠券
        for (Activity activitys:activityList
             ) {
            if(activitys.getType()==2)
            {
                String[] a = activitys.getCouponIds().split(",");
                List<Coupon> couponList=new ArrayList<>();
                Random random = new Random();
                int sj = random.nextInt(a.length);
                    couponList.add(this.couponService.getCouponByCouponId(Integer.parseInt(a[sj])));

                activitys.setCoupons(couponList);
                 }
            if(activitys.getType()==3)
            {
                String[] a = activitys.getCardId().split(",");
                List<Card> couponList=new ArrayList<>();
//                for (String a1:a
//                ) {
//                    couponList.add(this.cardService.queryCardByCardId(Integer.parseInt(a1)));
//                }
                Random random = new Random();
                int sj = random.nextInt(a.length);
                couponList.add(this.cardService.queryCardByCardId(Integer.parseInt(a[sj])));
                activitys.setCards(couponList);
            }
        }
        activityMap.put("activity",activityList);
        return activityMap;
    }
}
